﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhongBan : Form
    {
        private string _maPhongBan;

        public ThemPhongBan()
        {
            InitializeComponent();
        }

        public ThemPhongBan(string maPhongBan)
        {
            InitializeComponent();
            _maPhongBan = maPhongBan;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.PhongBan pb;
                
                if (_maPhongBan == null)
                    pb = new _DB.PhongBan();
                else
                    pb = dc.PhongBans.Single(x => x.MaPhongBan == _maPhongBan);

                pb.TenPhongBan = txtTenPhongBan.Text;
                pb.MaPhongBan = txtMaPhongBan.Text;
                pb.MoTa = txtMoTa.Text;

                if (_maPhongBan == null)
                    dc.PhongBans.InsertOnSubmit(pb);

                dc.SubmitChanges();
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }

        private void ThemPhongBan_Load(object sender, EventArgs e)
        {
            if (_maPhongBan == null)
                return;

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhongBan pb = dc.PhongBans.Single(x => x.MaPhongBan == _maPhongBan);
            txtMaPhongBan.Text = pb.MaPhongBan;
            txtMaPhongBan.ReadOnly = true;
            txtTenPhongBan.Text = pb.TenPhongBan;
            txtMoTa.Text = pb.MoTa;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
